package com.infore.operation.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2017/9/15.
 */
public class FileUtil {
	// 判断文件是否存在
	public static void judeFileExists(File file) throws IOException {

		if (file.exists()) {
			System.out.println("file exists");
		} else {
			System.out.println("file not exists, create it ...");
			file.mkdirs();
		}

	}

	//
	public static void createFile(String address, byte[] bytes, String fileName) throws IOException {
		File file = new File(address + "/" + fileName);
		FileOutputStream fos = new FileOutputStream(file);
		fos.write(bytes);
		fos.flush();
		fos.close();
	}

	// 将二进制编码转化为图片
	public static String getFile(byte[] bfile, String filePath, String fileName) {
		if (bfile != null) {
			BufferedOutputStream bos = null;
			FileOutputStream fos = null;
			File file = null;
			try {
				File dir = new File(filePath);
				if (!dir.exists()) {// 判断文件目录是否存在
					dir.mkdirs();
				}
				file = new File(filePath + "\\" + fileName);
				if (file.exists()) {
					file.delete();
				} 
					try {
						file.createNewFile();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				fos = new FileOutputStream(file);
				bos = new BufferedOutputStream(fos);
				bos.write(bfile);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (bos != null) {
					try {
						bos.close();
					} catch (IOException e1) {
						e1.printStackTrace();
					}

				}
				if (fos != null) {
					try {
						fos.close();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
			return filePath + "\\" + fileName;
		}
		return null;
	}
}
